/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.managecontrollers;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.PageDao;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Page;

/**
 * This controller shows all the static pages or redirects to the add/edit page;
 * Remind that the naming of add was not necessary because it's not possible to
 * add a new page.
 *
 * @author Sander
 */
public class ManagePageController extends HttpServlet {

    /**
     * This doGet checks wether it must show all the static pages or redirects
     * to the add/edit page where it is only possible to edit a page; See the
     * description of the controller for more information.
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher;
        HibernateUtil hu = new HibernateUtil();
        PageDao dao = new PageDao(hu.openSession());

        if (request.getParameter("edit") != null) { // Edit a page
            Page page = dao.get(Long.parseLong(request.getParameter("edit")));

            request.setAttribute("page", page);
            request.setAttribute("pagetoedit", true);

            // Breadcrumbs
            Breadcrumbs[] breadcrumbs = {
                new Breadcrumbs("Index", "Home"),
                new Breadcrumbs("ManagePage", "Manage pages"),
                new Breadcrumbs("ManagePage?edit", "Edit page")
            };
            request.setAttribute("breadcrumbs", breadcrumbs);

            dispatcher = request.getRequestDispatcher("jsp/add_edit_pages.jsp");
            dispatcher.forward(request, response);
        } else { // Shows all the static pages
            List<Page> pages = (List<Page>) dao.findAll();

            request.setAttribute("pages", pages);

            // Breadcrumbs
            Breadcrumbs[] breadcrumbs = {
                new Breadcrumbs("Index", "Home"),
                new Breadcrumbs("ManagePage", "Manage pages")
            };
            request.setAttribute("breadcrumbs", breadcrumbs);

            hu.close();

            dispatcher = request.getRequestDispatcher("jsp/manage_pages.jsp");
            dispatcher.forward(request, response);
        }
    }

    /**
     * This doPost creates or updates a page.
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String title = request.getParameter("title");
        String content = request.getParameter("content");

        HibernateUtil hu = new HibernateUtil();
        PageDao dao = new PageDao(hu.openSession());

        // Saving the page (add/edit)
        if (request.getParameter("pageid") != null) {
            Page page = dao.get(Long.parseLong(request.getParameter("pageid")));
            page.setTitle(title.substring(0, 1).toUpperCase() + title.substring(1));
            page.setContent(content);
            dao.update(page);
        }

        hu.close();

        response.sendRedirect("ManagePage");
    }
}
